//******************************************************************************
// @File: cyzEventAction.cc
// @Author: Cyz
// @Date: 2020年 5月 4日 星期一 17时47分39秒 CST
// @Description: 
//		Implementation of the cyzEventAction class
//******************************************************************************

#include "cyzEventAction.hh"
#include "cyzAnalysis.hh"

#include "G4Event.hh"
#include "G4RunManager.hh"
#include "G4SystemOfUnits.hh"
//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......

cyzEventAction::cyzEventAction()
: G4UserEventAction(),
  fEventID(0),
	fEdep_H(0),
	fEdep_B(0),
	ifInitDirection(true)
{
	fInitDirection = G4ThreeVector(0,0,0);
	fEcenter_H = G4ThreeVector(0,0,0);
	fEcenter_B = G4ThreeVector(0,0,0);
} 

//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......

cyzEventAction::~cyzEventAction()
{}

//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......

void cyzEventAction::BeginOfEventAction(const G4Event* event)
{    
	//
	// Initialization of variables
	//
	ifInitDirection = true;
  fEdep_H = 0.;
	fEdep_B = 0.;
	fEventID = event->GetEventID();
	fInitDirection = G4ThreeVector(0,0,0);
	fEcenter_H = G4ThreeVector(0,0,0);
	fEcenter_B = G4ThreeVector(0,0,0);
}

//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......

void cyzEventAction::EndOfEventAction(const G4Event*)
{
	G4AnalysisManager* analysisManager = G4AnalysisManager::Instance();
	/********  Don't write no scoring events  ********/
	if(fEdep_H <= 0.000000001*eV && fEdep_B <= 0.000000001*eV) return;
	
	/********  Fill ID  ********/
	analysisManager->FillNtupleIColumn(0,fEventID);

	/********  Fill Diraction  ********/
	analysisManager->FillNtupleDColumn(1,fInitDirection.phi()/CLHEP::pi);
	analysisManager->FillNtupleDColumn(2,fInitDirection.cosTheta());
	
	/********  Fill Energy Deposition  ********/
	analysisManager->FillNtupleDColumn(3,fEdep_H/MeV);
	analysisManager->FillNtupleDColumn(4,fEdep_B/MeV);

	/********  Fill Center of Gravity of Energy Depsition  ********/
	fEcenter_H = fEcenter_H/fEdep_H;
	analysisManager->FillNtupleDColumn(5,fEcenter_H.getX()/cm);
	analysisManager->FillNtupleDColumn(6,fEcenter_H.getY()/cm);
	analysisManager->FillNtupleDColumn(7,fEcenter_H.getZ()/cm);

	fEcenter_B = fEcenter_B/fEdep_B;
	analysisManager->FillNtupleDColumn(8,fEcenter_B.getX()/cm);
	analysisManager->FillNtupleDColumn(9,fEcenter_B.getY()/cm);
	analysisManager->FillNtupleDColumn(10,fEcenter_B.getZ()/cm);
	
	/********  Create New Row for Ntuple  ********/
	analysisManager->AddNtupleRow();
}

//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
